package org.fengdi.account.controller;

import lombok.extern.slf4j.Slf4j;
import org.fengdi.account.entity.Account;
import org.fengdi.account.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;


@Slf4j
@RestController
@RequestMapping("/api/account")
public class AccountController {
    @Autowired
    private AccountService accountService;

    // 减少账户余额
    @PostMapping
    public ResponseEntity<Boolean> decreaseCount(@RequestParam("user_id") String user_id, @RequestParam("money") double money) {
        try {
            this.accountService.decreaseMoney(money, user_id);
        }catch (Exception e){
            log.error(e.getMessage());
            return new ResponseEntity<>(false, HttpStatus.INTERNAL_SERVER_ERROR);
        }

        return new ResponseEntity<>(true, HttpStatus.OK);
    }

    // 获取账户信息
    @GetMapping
    public Account getAccountByUserId(@RequestParam("user_id") String userId) {
        return this.accountService.getAccountByUserId(userId);
    }

}
